//
//  PersonCustomerTableViewCell.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/8/2.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class PersonCustomerTableViewCell: WZC_TableViewCell {

    var model:PersonCustomModel?
    
    
    lazy var gridView : UICollectionView = {
        
        let layout = UICollectionViewFlowLayout.init()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = 0.6
        layout.minimumLineSpacing = 1
        let width:CGFloat = kScreenW/4
        layout.itemSize = CGSize.init(width: width-2, height:90)
        
        let collectionView = UICollectionView.init(frame:.zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.clear
        collectionView.register(PersonCustomerCollectionViewCell.self, forCellWithReuseIdentifier: "PersonCustomerCollectionViewCell")
        collectionView.isScrollEnabled = false;
        self.contentView.addSubview(collectionView)
        
        return collectionView
    }()
    

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.hexColor(hexadecimal: "#ffffff")
        
        self.model = loadData()
        
        gridView.snp.makeConstraints { (make) in
            
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(180)
        }
    }
    
    func loadData() -> PersonCustomModel? {
        
        let path = Bundle.main.path(forResource: "Items", ofType: "json")
        // 2. 获取json文件里面的内容,NSData格式
        let jsonData=NSData(contentsOfFile: path!)
        // 3. 解析json内容
        
        do {
            
            let json = try JSONSerialization.jsonObject(with: jsonData! as Data, options: .allowFragments)
            
            if let result = json as? Dictionary<String, Any> {
                
                return  PersonCustomModel.deserialize(from: result)
                
            }
            else {
                
                return nil
            }
            
        } catch  {
            
            print("解析失败")
            return nil
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension PersonCustomerTableViewCell:UICollectionViewDelegate,UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return model?.data?.count ?? 0
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:PersonCustomerCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "PersonCustomerCollectionViewCell", for: indexPath) as! PersonCustomerCollectionViewCell
        let tmodel = model?.data?[indexPath.row]
        
        cell.itemView.titleLabel.text = tmodel?.name
        
        cell.itemView.bgImgView.image = IMAGE("mjyp_set_icon_collection")
       
        return cell
    }
    
}
